package de.zainodis.commons.utils;

import java.util.List;

import android.text.InputType;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import de.zainodis.commons.model.SelectMode;

/**
 * Class providing different user interface related utility methods.
 * 
 * @author fzarrai
 * 
 */
public final class UserInterfaceUtils {

   private static final String UPPERCASE = "A";
   private static final String LOWERCASE = "o";
   private static final String NUMERIC = "5";
   private static final String ALPHA_NUMERIC = "5aBc";

   private static final String TAG = "UserInterfaceUtils";

   /**
    * A little hack if you want to embed a {@link ListView} in a
    * {@link ScrollView}. Officially this is not supported - amongst other
    * reasons, because a listview already has a scrollbar. However there are
    * cases where a listview needs to embedded in scrollview with other ui
    * components. For those cases this method calculates the correct height of
    * the listview and sets it accordingly on the provided listview. This method
    * always needs to be called directly after modifying listview items. This
    * only works, if your list is backed by a {@link BaseAdapter} (listadapter).
    * Otherwise nothing is done.
    * 
    * @param listView
    *           the {@link ListView} whose height should be (re-calculated.
    */
   public static void setListViewHeightBasedOnChildren(ListView listView) {
	 ListAdapter listAdapter = listView.getAdapter();
	 if (listAdapter != null) {
	    int totalHeight = 0;
	    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
	    for (int i = 0; i < listAdapter.getCount(); i++) {
		  View listItem = listAdapter.getView(i, null, listView);
		  listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
		  totalHeight += listItem.getMeasuredHeight();
	    }

	    ViewGroup.LayoutParams params = listView.getLayoutParams();
	    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
	    listView.setLayoutParams(params);
	    listView.requestLayout();
	 }
   }

   /**
    * Determines a suitable {@link InputType} for the soft keyboard, based on
    * the given regular expression.
    * 
    * @return a suitable input type.
    */

   public static int getInputType(String regex) {

	 boolean isUpperCase = RegexUtils.canParse(regex, UPPERCASE);
	 boolean isLowerCase = RegexUtils.canParse(regex, LOWERCASE);
	 boolean isNumeric = RegexUtils.canParse(regex, NUMERIC);
	 boolean isAlphaNumeric = RegexUtils.canParse(regex, ALPHA_NUMERIC);

	 if (isUpperCase && !isLowerCase) {
	    return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
	 } else if (isNumeric && (!isUpperCase && !isLowerCase && !isAlphaNumeric)) {
	    return InputType.TYPE_CLASS_NUMBER;
	 }
	 return InputType.TYPE_CLASS_TEXT;
   }

   public static void setSelectMode(SelectMode mode, TextView target) {
	 switch (mode) {
	 case Replace:
	    target.setSelectAllOnFocus(true);
	    break;
	 case Insert:
	    target.setSelectAllOnFocus(false);
	    break;
	 }
   }
}
